package wxdb.ui;



import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Login extends JDialog{
	
	private JButton loginButton = new JButton("Login");
	private JButton registerButton = new JButton("Register");
	private JButton cancelButton = new JButton("Cancel");
	
	private JLabel userLabel = new JLabel("User :");
	private JLabel passwdLabel = new JLabel("Password :");
	
	private JTextField userInput = new JTextField(10);
	private JPasswordField passwdInput = new JPasswordField(10);
	
	private JPanel pane = new JPanel();
	
	Font fon = new Font("Dialog",Font.PLAIN,15);
	
	
	void buildConstraints(GridBagConstraints gbc,int gx, int gy, int gw, int gh, int wx, int wy){
		gbc.gridx = gx;			
		gbc.gridy = gy;			
		gbc.gridwidth = gw;		
		gbc.gridheight = gh;	
		gbc.weightx = wx;		
		gbc.weighty = wy;		
	}
	
	
	private void setMyPane(){
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		pane.setLayout(gridbag);
		
		buildConstraints(constraints,0,0,3,1,1,1);
		gridbag.setConstraints(userLabel, constraints);
		pane.add(userLabel);
		
		buildConstraints(constraints,3,0,3,1,1,1);
		gridbag.setConstraints(userInput, constraints);
		userInput.setFont(fon);
		pane.add(userInput);
		
		buildConstraints(constraints,0,1,3,1,1,1);
		gridbag.setConstraints(passwdLabel, constraints);
		pane.add(passwdLabel);
		
		buildConstraints(constraints,3,1,3,1,1,1);
		gridbag.setConstraints(passwdInput, constraints);
		passwdInput.setFont(fon);
		pane.add(passwdInput);
		
		buildConstraints(constraints,0,2,2,1,1,1);
		gridbag.setConstraints(loginButton, constraints);
		pane.add(loginButton);

		buildConstraints(constraints,2,2,2,1,1,1);
		gridbag.setConstraints(cancelButton, constraints);
		pane.add(cancelButton);
		
		buildConstraints(constraints,4,2,2,1,1,1);
		gridbag.setConstraints(registerButton, constraints);
		pane.add(registerButton);
		
		setLocation(200,80);
		setContentPane(pane);
	}
	

	private void initListener(){
		ActListener actListener = new ActListener();
		loginButton.addActionListener(actListener);
		cancelButton.addActionListener(actListener);
		registerButton.addActionListener(actListener);
	}
	
	class ActListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == loginButton)
				loginRun();
			if(e.getSource() == cancelButton)
				cancelRun();
			if(e.getSource() == registerButton)
				registerRun();
		}
	}
	
	private void registerRun(){
		if(Global.getDBCount() >= Global.UI_REGISTER_DBMAX)
			return;
		Register reg = new Register();
	}
	
	private void cancelRun(){
		this.dispose();
	}
	
	private void loginRun(){
		String usr = userInput.getText();
		String pwd = new String(passwdInput.getPassword());
		if(usr.equals("")){
			JOptionPane.showMessageDialog(null,"Inpute your username please","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(pwd.equals("")){
			JOptionPane.showMessageDialog(null,"Inpute your passwd please","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(Global.isUserExist(usr)==false){
			JOptionPane.showMessageDialog(null,"No this user","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(pwd.equals(Global.getUserPasswd(usr))==false){
			JOptionPane.showMessageDialog(null,"password error","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(usr.equals("Administrator")){
			Admin ad = new Admin();
			this.dispose();
		}else{
			try{
				SelectDB sdb = new SelectDB(usr);
				this.dispose();
			}catch(Exception e){};
		}
	}
	
	public Login(){
		setTitle("Login");
		setSize(500,200);
		initListener();
		setMyPane();
		setVisible(true);
		setResizable(false);
	}
}
